package com.barbeque.mall.service;

import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.bean.region.*;
import com.barbeque.mall.mapper.MarketRegionMapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Tezel
 * @since 2022/07/15 20:57
 */

@Service
public class RegionServiceImpl implements RegionService {

    @Autowired
    MarketRegionMapper regionMapper;

    @Override
    public CommonData<MarketRegion> query() {

        MarketRegionExample example = new MarketRegionExample();
        MarketRegionExample.Criteria criteria = example.createCriteria();
        //查找省信息
        criteria.andIdBetween(1, 31);

        List<MarketRegion> marketRegions = regionMapper.selectByExample(example);



        for (MarketRegion marketRegion : marketRegions) {
            Integer id = marketRegion.getId();
            MarketRegionExample example2 = new MarketRegionExample();
            MarketRegionExample.Criteria criteria2 = example2.createCriteria();
            criteria2.andPidEqualTo(id);


            List<MarketRegion> marketCityRegions = regionMapper.selectByExample(example2);

            for (MarketRegion marketCityRegion : marketCityRegions) {
                Integer cityId = marketCityRegion.getId();
                MarketRegionExample example3 = new MarketRegionExample();
                MarketRegionExample.Criteria criteria3 = example3.createCriteria();
                criteria3.andPidEqualTo(cityId);
                //市id = 区pid
                List<MarketRegion> marketAreaRegions = regionMapper.selectByExample(example3);
                marketCityRegion.setChildren(marketAreaRegions);
            }

            marketRegion.setChildren(marketCityRegions);

        }


        PageHelper.startPage(1, 31);
        PageInfo<MarketRegion> pageInfo = new PageInfo<>(marketRegions);

        return CommonData.data(pageInfo);

    }
}
